package com.cyl.config.ecxle;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.ArrayUtils;

import com.cyl.config.ecxle.annotation.ExcelSysDictAnnotation;
import com.cyl.manager.act.domain.entity.CouponCategory;
import com.cyl.manager.act.service.CouponCategoryService;
import com.ruoyi.common.annotation.Excel;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.framework.config.LocalDataUtil;
import com.ruoyi.system.service.ISysDeptService;

import io.swagger.annotations.ApiModelProperty;

public class ExcelSysDictOutUtils {
	
	private static Map<String, List<Map<String, String>>> CLASS_FIELD_MAP =  new HashMap<String, List<Map<String, String>>>();
	
	
    
	/**
	 *  	获取缓存信息
	 * @return
	 */
	private static Map<String, List<Map<String, String>>> classFieldMap(){
		return CLASS_FIELD_MAP;
	}
	
	
	/**
	 * 	到处ecxle字典翻译设置
	 * @param ent
	 */
	public static Map<String, Map<String,String>> getSysDictMap(String dicts){
		   Map<String, Map<String,String>> sysDictMap = new HashMap<String, Map<String,String>>();
		   String[] dictArray = dicts.split(",");
		   for (String sys_dict_type : dictArray) {
			   	   Map<String, String> sys_dict_data = new HashMap<String, String>();
				   if("SYS_DEPT_DPBM_DICT".equals(sys_dict_type)) {
				       ISysDeptService sysDeptService = SpringUtils.getBean(ISysDeptService.class);
				       SysDept sysDeptQr = new SysDept();
				       sysDeptQr.setBmlx("5");
					   List<SysDept> dpSysDepts = sysDeptService.selectDeptList(sysDeptQr);
					   if(dpSysDepts!=null) {
				    	   for (SysDept sysDept : dpSysDepts) {
				    		   sys_dict_data.put(sysDept.getDeptId()+"", sysDept.getDeptName());
				    	   }
				       }
				   }else if("SYS_DEPT_DPSH_DICT".equals(sys_dict_type)) {
				       ISysDeptService sysDeptService = SpringUtils.getBean(ISysDeptService.class);
				       SysDept sysDeptQr = new SysDept();
				       sysDeptQr.setBmlx("5");
					   List<SysDept> dpSysDepts = sysDeptService.selectDeptList(sysDeptQr);
					   if(dpSysDepts!=null) {
				    	   for (SysDept sysDept : dpSysDepts) {
				    		   sys_dict_data.put(sysDept.getDeptId()+"", getShxx(sysDept));
				    	   }
				       }
				   }else {
					   List<SysDictData> sys_dict_datas = DictUtils.getDictCache(sys_dict_type);
				       if(sys_dict_datas!=null) {
				    	   for (SysDictData sysDictData : sys_dict_datas) {
				    		   sys_dict_data.put(sysDictData.getDictValue(), sysDictData.getDictLabel());
				    	   }
				       }
				   }
		   }
	       LocalDataUtil.setVar(Constants.SYS_DICT_TYPE,sysDictMap);
		   return sysDictMap;
		 
	}
	
	private static String getShxx(SysDept sysDept) {
		if(sysDept==null) {
			return "";
		}
		return "收货人："+sysDept.getLeader()+";联系电话："+sysDept.getPhone()+";收货地址："+sysDept.getProvince()+"-"+sysDept.getCity()+"-"+sysDept.getDistrict()+"-"+sysDept.getAddress();
	}

	/**
	 * 	到处ecxle字典翻译设置
	 * @param ent
	 */
	public static void setSysDictMap(Class<?> ent){
		   Map<String, Map<String,String>> sysDictMap = new HashMap<String, Map<String,String>>();
		   Map<String, String> field_name_data = new HashMap<String, String>();

		   List<Map<String, String>> list =  getQueryField(ent);
		   for (Map<String, String> map : list) {
			   if(map.containsKey("value")) {
				   Map<String, String> sys_dict_data = new HashMap<String, String>();
				   String sys_dict_type = map.get("value");
				   String fieldName = map.get("name");
				   field_name_data.put(fieldName, sys_dict_type);
				   if("SYS_DEPT_DPBM_DICT".equals(sys_dict_type)) {
				       ISysDeptService sysDeptService = SpringUtils.getBean(ISysDeptService.class);
				       SysDept sysDeptQr = new SysDept();
				       sysDeptQr.setBmlx("5");
					   List<SysDept> dpSysDepts = sysDeptService.selectDeptList(sysDeptQr);
					   if(dpSysDepts!=null) {
				    	   for (SysDept sysDept : dpSysDepts) {
				    		   sys_dict_data.put(sysDept.getDeptId()+"", sysDept.getDeptName());
				    	   }
				       }
				   }else if("SYS_DEPT_DPSH_DICT".equals(sys_dict_type)) {
				       ISysDeptService sysDeptService = SpringUtils.getBean(ISysDeptService.class);
				       SysDept sysDeptQr = new SysDept();
				       sysDeptQr.setBmlx("5");
					   List<SysDept> dpSysDepts = sysDeptService.selectDeptList(sysDeptQr);
					   if(dpSysDepts!=null) {
				    	   for (SysDept sysDept : dpSysDepts) {
				    		   sys_dict_data.put(sysDept.getDeptId()+"", getShxx(sysDept));
				    	   }
				       }
				   }else  if("OMS_COUP_CATE_DICT".equals(sys_dict_type)) {
					   CouponCategoryService couponCategoryService = SpringUtils.getBean(CouponCategoryService.class);
					   List<CouponCategory>  couponCategorys = couponCategoryService.selectListAll();
					   if(couponCategorys!=null) {
				    	   for (CouponCategory couponCategory : couponCategorys) {
				    		   sys_dict_data.put(couponCategory.getId()+"", couponCategory.getTitle());
				    	   }  
				       }
				   }else  if("pms_product_lx".equals(sys_dict_type)) {
		    		   sys_dict_data.put("1", "正常商品");
		    		   sys_dict_data.put("2", "福利券商品");
		    		   sys_dict_data.put("3", "积分商品");
				   } else {
					   List<SysDictData> sys_dict_datas = DictUtils.getDictCache(sys_dict_type);
				       if(sys_dict_datas!=null) {
				    	   for (SysDictData sysDictData : sys_dict_datas) {
				    		   sys_dict_data.put(sysDictData.getDictValue(), sysDictData.getDictLabel());
				    	   }
				       }
				   }
			
				   sysDictMap.put(sys_dict_type, sys_dict_data);
			   }
		   }
		   sysDictMap.put("SYS_FIELD_NAME_DATA1233212", field_name_data);

	       LocalDataUtil.setVar(Constants.SYS_DICT_TYPE,sysDictMap);
	}
	
	/**
	 * 	获取有查询条件的字段
	 * @param ent
	 * @return
	 */
	private static List<Map<String, String>>  getQueryField(Class<?> ent){
		List<Map<String, String>> retFields = new ArrayList<Map<String, String>>();
		if(ent!=null) {
			String typeName =  ent.getTypeName();
			if(false || CLASS_FIELD_MAP.containsKey(typeName)) {
				return CLASS_FIELD_MAP.get(typeName);
			}else {
				Field[] thisFields = ent.getDeclaredFields(); 
				Field[] superFields = ent.getSuperclass().getDeclaredFields(); 
				Field[] fields = (Field[]) ArrayUtils.addAll(thisFields, superFields);


				if(fields!=null && fields.length>0) {
					for (Field field : fields) {
						  boolean fieldRemarkBool = field.isAnnotationPresent(ExcelSysDictAnnotation.class); 
						  if(fieldRemarkBool) {
							  ExcelSysDictAnnotation fieldRemark =  field.getAnnotation(ExcelSysDictAnnotation.class);  
			            		 if(fieldRemark!=null) {
			            			 Map<String, String> map = new HashMap<String, String>();
			            			 map.put("name", field.getName());
			            			 map.put("value", fieldRemark.value());
			            			 retFields.add(map);
			            		 }
						  }
					}
				}
				CLASS_FIELD_MAP.put(typeName, retFields);
			}
		}
		
		return retFields;
	}
	 
}